package com.yadong.smarthome2.protocol2.client;

import com.yadong.smarthome2.protocol2.Protocol;
import com.yadong.smarthome2.protocol2.RequestProtocol.Request;
import com.yadong.smarthome2.protocol2.ResultProtocol.Function;
import com.yadong.smarthome2.protocol2.ResultProtocol.Result;

public abstract class FunctionRunable implements Runnable {
    protected Function mFunction;
    protected Request mRequest;
    protected Result.Builder mResultBuilder;

    protected void safeRun() {
        try {
            run();
        } catch (Exception e) {
            e.printStackTrace();
            mResultBuilder.addResult(Protocol.RESULT_FAILD);
            mResultBuilder.addResult("error parameter");
        }
    }

    public FunctionRunable(Function function) {
        mFunction = function;
    }

    public void runInternal(Request request, Result.Builder result){
        mRequest = request;
        mResultBuilder = result;
        safeRun();
    }
}
